<?php

class Mindex extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get16post() {
        $this->db->where("topic_news_status", "1");
        $this->db->order_by("topic_news_number", "ASC");
        $query = $this->db->get('topic_news');
        return $query->result();
    }

    function get5media() {
        $this->db->where("topic_news_status", "1");
        $this->db->where("topic_news_type", "video");
        $this->db->or_where("topic_news_type", "picture");
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->limit(5, 0);
        $query = $this->db->get("topic_news");
        if($query->num_rows > 0) {
             return $query->result();
        } else {
            return false;
        }
    }
    function get5picture(){
        $this->db->where("topic_news_status", "1");
        $this->db->where("topic_news_type", "picture");
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->limit(5, 0);
        $query = $this->db->get("topic_news");
        if($query->num_rows > 0) {
             return $query->result();
        } else {
            return false;
        }
    }
    function get3video(){
        $this->db->where("topic_news_status", "1");
        $this->db->where("topic_news_type", "video");
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->limit(3, 0);
        $query = $this->db->get("topic_news");
        if($query->num_rows > 0) {
             return $query->result();
        } else {
            return false;
        }
    }
    function get5new() {
        $this->db->where("topic_news_status", "1");
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->limit(5, 0);
        $query = $this->db->get("topic_news");
        if($query->num_rows > 0) {
             return $query->result();
        } else {
            return false;
        }
    }
    function gettop() {
        $this->db->order_by("topic_news_id", "DESC");
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        return $query->result_array();
    }
    function get5mediaByCate($id) {
        $this->db->where("category_id", $id);
        $this->db->where("topic_news_status", "1");
        $this->db->where("topic_news_type", "video");
        $this->db->or_where("topic_news_type", "picture");
        $this->db->limit(5, 0);
        $query = $this->db->get("topic_news");
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicTop1ByCate($id){
        $this->db->where("topic_news_number",1);
        $this->db->where("topic_news_status", "1");
        $this->db->where("category_id", $id);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->row();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicTop1BySub($id){
        $this->db->where("topic_news_number",1);
        $this->db->where("topic_news_status", "1");
        $this->db->where("sub_category_id", $id);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->row();
            return $data;
        } else {
            return false;
        }
    }
    function listTopicBySubId($sub_cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicBySubCate($cate_id,$sub_cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->where("category_id",$cate_id);
        $this->db->limit(3,0);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicBySub($sub_cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function get5TopicBySubId($sub_cate_id,$topic_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->where("topic_news_status", "1");
        $this->db->where("topic_news_id <", $topic_id);
        $this->db->limit(5,0);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicReadlessBySub($sub_cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->order_by("total_view", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->limit(5,0);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicReadless(){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->order_by("total_view", "DESC");
        $this->db->limit(5,0);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function get_collate($str)
    {
        $encoding = strtolower(mb_detect_encoding($str));
        $collate = ($encoding == 'utf-8')?' COLLATE utf8_bin ':'';
        return $collate;
    } 
    function getTopicTag($topic_id,$tagName){
        $collate = $this->get_collate($tagName);
        $tagName = $this->db->escape_str($tagName);
        $tagName = strtolower($tagName);
        if($tagName == ""){
            return false;
        }
        $tag = explode(',', $tagName); 
        $string  = array();
        for($i=0; $i<count($tag); $i++) {
            $string[] = " (LOWER(topic_news_tag) $collate LIKE LOWER('%".$tag[$i]."%'))
                OR (LOWER(topic_news_title) $collate LIKE LOWER('%".$tag[$i]."%')) ";
        }
        if(count($string) ==1){
            $where = $string[0];
        }else $where = implode(' OR ', $string);
        $sql = "SELECT * FROM topic_news
                WHERE topic_news_status=1 AND topic_news_id <> $topic_id AND ".$where."
                ORDER BY topic_news_date desc LIMIT 0,4
        ";

        $query = $this->db->query($sql);
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function get5TopicBySub($cate_id,$sub_cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("sub_category_id",$sub_cate_id);
        $this->db->where("category_id",$cate_id);
        $this->db->limit(5,0);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function get5TopicByCate($cate_id){
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("category_id",$cate_id);
        $this->db->limit(5,0);
        $this->db->where("topic_news_status", "1");
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicTop3ByCate($id){
        $this->db->where("topic_news_number >",1);
        $this->db->where("topic_news_number < ",4);
        $this->db->where("topic_news_status", "1");
        $this->db->where("category_id", $id);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicTop3BySub($id){
        $this->db->where("topic_news_number >",1);
        $this->db->where("topic_news_number < ",4);
        $this->db->where("topic_news_status", "1");
        $this->db->where("sub_category_id", $id);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getTopicByCate($id) {
        $this->db->order_by("topic_news_date", "DESC");
        $this->db->where("topic_news_status", "1");
        $this->db->where("category_id", $id);
        $query = $this->db->get('topic_news');
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function getSearch($keyword,$type,$start, $limit) {
        $collate = $this->get_collate($keyword);
        $keyword = $this->db->escape_str($keyword);
        $keyword = strtolower($keyword);
        if($type==1){
            // tim kiem theo tieu de
            $sql = "SELECT * FROM topic_news
                    WHERE topic_news_status=1 AND (LOWER(topic_news_title) $collate LIKE LOWER('%$keyword%'))
                    ORDER BY topic_news_id desc LIMIT $start, $limit
            ";
        }elseif ($type==3) { 
            // tim kiem theo noi dung
            $sql = "SELECT * FROM topic_news
                    WHERE topic_news_status=1 AND (LOWER(topic_news_content) $collate LIKE LOWER('%$keyword%'))
                    
                    ORDER BY topic_news_id desc LIMIT $start, $limit
            ";
        }else {
            // tim kiem theo tat ca
            $sql = "SELECT * FROM topic_news
                    WHERE topic_news_status=1 AND (LOWER(topic_news_title) $collate LIKE LOWER('%$keyword%'))
                    OR (LOWER(topic_news_content) $collate LIKE LOWER('%$keyword%'))
                    
                    ORDER BY topic_news_id desc LIMIT $start, $limit
            ";

        }
        $query = $this->db->query($sql);
        if($query->num_rows > 0) {
            $data = $query->result();
            return $data;
        } else {
            return false;
        }
    }
    function totalgetSearch($keyword,$type) {
        $collate = $this->get_collate($keyword);
        $keyword = $this->db->escape_str($keyword);
        $keyword = strtolower($keyword);
        if($type ==1){
            //tong tim kiem theo tieu de
            $sql = "SELECT count(*) AS total FROM topic_news as a
                    WHERE a.topic_news_status=1 AND (LOWER(a.topic_news_title) $collate LIKE LOWER('%$keyword%'))
            ";
        }elseif ($type==3) {
            # tong tim kiem theo noi dung
            $sql = "SELECT count(*) AS total FROM topic_news as a
                    WHERE a.topic_news_status=1 AND (LOWER(a.topic_news_content) $collate LIKE LOWER('%$keyword%'))
            ";
        }else{
            // tim kiem all
            $sql = "SELECT count(*) AS total FROM topic_news as a
                    WHERE a.topic_news_status=1 AND (LOWER(a.topic_news_title) $collate LIKE LOWER('%$keyword%'))
                    OR (LOWER(a.topic_news_content) $collate LIKE LOWER('%$keyword%'))
            ";
        }
        

        $query = $this->db->query($sql);
        if($query->num_rows > 0) {
            return $query->row()->total;
        } else {
            return 0;
        }
    }
    function getInfoTopic($id) {
        $this->db->where('topic_news_id', $id);
        $query = $this->db->get('topic_news');
        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return false;
        }
    }
    function updateCountView($id){
        $this->db->query('update topic_news set total_view=total_view+1 where topic_news_id=' . $id);
    }
}
